package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.secsys.dto.BbsPostAuthorDTO;
import com.secsys.dto.SysUserDTO;
import com.secsys.model.TbSysUser;
import com.secsys.service.UserService;

/**
 * @className userServiceImpl
 * @Description 用户业务实现类
 * @author LiusQ
 * @date 2011-3-28 16:23
 * 
 */
public class UserServiceImpl extends ServiceImpl<TbSysUser, Integer> implements
		UserService {
	
	private RoleServiceImpl roleService;

	/**
	 * @Description 将SysUserDTO对象转换成TbSysUser对象
	 * @param userDTO
	 *            SysUserDTO对象
	 * @return TbSysUser对象
	 */
	public TbSysUser getBeanFromDTO(SysUserDTO userDTO) {
		TbSysUser tbSysUser = new TbSysUser();
		tbSysUser.setId(userDTO.getId());
		tbSysUser.setUserName(userDTO.getUserName());
		tbSysUser.setEmail(userDTO.getEmail());
		if (userDTO.getBrithday() != null) {
			tbSysUser.setBrithday(userDTO.getBrithday());
		}
		if (userDTO.getCareer() != null)
			tbSysUser.setCareer(userDTO.getCareer());
		 if(userDTO.getCity()!=null)
		tbSysUser.setCity(userDTO.getCity());
		 
		if (userDTO.getCompany() != null)
			tbSysUser.setCompany(userDTO.getCompany());
		tbSysUser.setLevel(userDTO.getLevel());
		 
		if (userDTO.getMsn() != null)
			tbSysUser.setMsn(userDTO.getMsn());
		if(userDTO.getOnlineStatue()!=null)
		tbSysUser.setOnlineStatue(userDTO.getOnlineStatue());
		tbSysUser.setPassword(userDTO.getPassword());
	     tbSysUser.setRegisterAddr(userDTO.getRegisterAddr());
		//tbSysUser.setProvince(userDTO.getProvince());
		tbSysUser.setRegisterTime(userDTO.getRegisterTime());
		//tbSysUser.setResume(userDTO.getResume());
		if(userDTO.getSex()!=null)
		tbSysUser.setSex(userDTO.getSex());
		if (userDTO.getQq() != null)
			tbSysUser.setQq(userDTO.getQq());
		tbSysUser.setUserStatue(userDTO.getUserStatue());
		if(userDTO.getRoleId()!=0 && String.valueOf(userDTO.getRoleId())!=""){
			tbSysUser.setTbSysRole(roleService.findById(userDTO.getRoleId()));
		}
		return tbSysUser;
	}
    /**
     * @Description  通过用户名查询用户信息
     */
	@Override
	public TbSysUser queryUserByName(String userName) {
		// TODO Auto-generated method stub
		 String hql="from TbSysUser user where user.userName=?";
		 List list=dao.find(hql, new Object[]{userName});
		 return list!=null&&list.size()>0?(TbSysUser)list.get(0):null;
	}
	/**
	 * @Description  通过用户名查询用户信息
	 */
	@Override
	public TbSysUser queryUserByEmail(String email) {
		String hql="from TbSysUser user where user.email=?";
		List list=dao.find(hql, new Object[]{email});
		return (list!=null&&list.size()>0)?(TbSysUser)list.get(0):null;
	}

	/**
	 * @Description 将TbSysUser 持久类转换成相应的DTO类
	 */
	public SysUserDTO getDTOFromBean(TbSysUser tbSysUser) {
		SysUserDTO userDTO = new SysUserDTO();
		userDTO.setId(tbSysUser.getId());
		userDTO.setUserName(tbSysUser.getUserName());
		userDTO.setEmail(tbSysUser.getEmail());
		if (tbSysUser.getBrithday() != null) {
			userDTO.setBrithday(tbSysUser.getBrithday());
		}
		if (tbSysUser.getCareer() != null)
			userDTO.setCareer(tbSysUser.getCareer());
		userDTO.setCity(tbSysUser.getCity());
		if (tbSysUser.getCompany() != null)
			userDTO.setCompany(tbSysUser.getCompany());
		userDTO.setLevel(tbSysUser.getLevel());
		if (tbSysUser.getMsn() != null)
			userDTO.setMsn(tbSysUser.getMsn());
		userDTO.setOnlineStatue(tbSysUser.getOnlineStatue());
		userDTO.setPassword(tbSysUser.getPassword());
		userDTO.setProvince(tbSysUser.getProvince());
		userDTO.setRegisterTime(tbSysUser.getRegisterTime());
		userDTO.setResume(userDTO.getResume());
		userDTO.setSex(tbSysUser.getSex());
		if (tbSysUser.getQq() != null)
			userDTO.setQq(tbSysUser.getQq());
		if(tbSysUser.getPhoto()!=null)
			userDTO.setPhoto(tbSysUser.getPhoto());
		userDTO.setUserStatue(tbSysUser.getUserStatue());
		if(tbSysUser.getTbSysRole()!=null){
			userDTO.setRoleId(tbSysUser.getTbSysRole().getId());
		}
		return userDTO;
	}

	/**
	 * @Description 將PO集合转换成VO 集合
	 * @param List
	 *            PO 集合
	 * @return conList VO 集合
	 */
	@Override
	public List<SysUserDTO> converObject(List<TbSysUser> List) {
		// TODO Auto-generated method stub
		List<SysUserDTO> conList = new ArrayList<SysUserDTO>(List.size());
		forEach(List, conList);
		return conList;
	}

	/**
	 * @Description 用户注册方法
	 * @param userDTO
	 *            用户注册信息封装类
	 */
	@Override
	public boolean  register(SysUserDTO userDTO) {
		boolean flag=false;
		try{ 
			TbSysUser sysUser = getBeanFromDTO(userDTO);
			this.dao.save(sysUser);
			flag=true;
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println(userDTO.getUserName()+"数据保存失败，注册失败!");
		} 
		return flag;
	}
    
	/**
	 * @param userName 用户名
	 * @param password 用户密码
	 * @Description  用户登录信息验证
	 * @return SysUserDTO 对象  
	 */
	@Override
	public SysUserDTO loginInfo(String password, String userName) {
		// TODO Auto-generated method stub
		  SysUserDTO userDTO=null;
		String hql="from TbSysUser user where user.userName=? and user.password=?";
		 List userList=this.dao.find(hql, new Object[]{userName,password});
	   
		  if(userList!=null&&userList.size()!=0){
			  userDTO=getDTOFromBean((TbSysUser)userList.get(0));
    
		  }
		return  userDTO;
	}
    
	
	public TbSysUser login(String password,String userName){
		  SysUserDTO userDTO=null;
			String hql="from TbSysUser user where user.userName=? and user.password=?";
			 List userList=this.dao.find(hql, new Object[]{userName,password});
		   
			 
			return  userList!=null&&userList.size()>0?(TbSysUser)userList.get(0):null;
	}
	
	/**
	 * 
	 * @Description  通过用户名查找用户信息 
	 * @param userName
	 * @return
	 */
   public TbSysUser findUserInfoByName(String userName){
	   String hql="from TbSysUser user where user.userName=?";
	   List userList=dao.find(hql, new Object[]{userName});
	   if(userList!=null&&userList.size()>0){
		   return (TbSysUser)userList.get(0);
	   }else{
		   return null;
	   }
   }
   
	/**
	 * 
	 * @Description 获取帖子版主 ，回帖用户一些基本情况
	 * @return
	 */
    public BbsPostAuthorDTO getAuthorInfo(Integer id){
    	 
    	 String hql="from TbSysUser user where user.id=?";
    	 List list=dao.find(hql,new Object[]{id});
    	 
    		 TbSysUser authorInfo=(TbSysUser)list.get(0);
    	 BbsPostAuthorDTO userDTO=new BbsPostAuthorDTO();
    	 userDTO.setUserName(authorInfo.getUserName());
    	 userDTO.setLevel(authorInfo.getLevel());
    	 userDTO.setId(authorInfo.getId());
    	 userDTO.setOnlineStatue(authorInfo.getOnlineStatue());
    	 userDTO.setPhoto(authorInfo.getPhoto());
    	 userDTO.setResume(authorInfo.getResume());
    	 userDTO.setSex(authorInfo.getSex());
    	 
    	 userDTO.setRoleName(authorInfo.getTbSysRole().getRole());
    	 userDTO.setPostCount(authorInfo.getTbBbsPosts().size());  //发帖总数
    	 userDTO.setReplyCount(authorInfo.getTbBbsReplies().size()); //回帖总数
    	 return userDTO;
    	 
    }
   
	@Override
	public void updateUserInfo(SysUserDTO userDTO) {
		// TODO Auto-generated method stub
         
	}
   
	/**
	 * @Description  删除用户信息
	 * @param sysUserDTO  用户信息封装类
	 */
	public void deleteUserInfo(SysUserDTO sysUserDTO){
		      this.dao.deleteByKey(sysUserDTO.getId());
	}
	
	/**
	 * @Description 將PO轉換成VO存到指定的集合
	 * @param list
	 *            PO集合
	 * @param conList
	 *            VO集合
	 * @return
	 */
	private void forEach(List<TbSysUser> list, List<SysUserDTO> conList) {
		SysUserDTO sysUserDTO = null;
		for (TbSysUser tbUser : list) {
			sysUserDTO = new SysUserDTO();
			sysUserDTO.setId(tbUser.getId());
			sysUserDTO.setUserName(tbUser.getUserName());
			sysUserDTO.setEmail(tbUser.getEmail());
			if (tbUser.getBrithday() != null) {
				sysUserDTO.setBrithday(tbUser.getBrithday());
			}
			if (tbUser.getCareer() != null)
				sysUserDTO.setCareer(tbUser.getCareer());
			sysUserDTO.setCity(tbUser.getCity());
			if (tbUser.getCompany() != null)
				sysUserDTO.setCompany(tbUser.getCompany());
			sysUserDTO.setLevel(tbUser.getLevel());
			if (tbUser.getMsn() != null)
				sysUserDTO.setMsn(tbUser.getMsn());
			sysUserDTO.setOnlineStatue(tbUser.getOnlineStatue());
			sysUserDTO.setPassword(tbUser.getPassword());
			sysUserDTO.setProvince(tbUser.getProvince());
			sysUserDTO.setRegisterTime(tbUser.getRegisterTime());
			sysUserDTO.setResume(tbUser.getResume());
			sysUserDTO.setSex(tbUser.getSex());
			if (tbUser.getQq() != null)
				sysUserDTO.setQq(tbUser.getQq());
			sysUserDTO.setUserStatue(tbUser.getUserStatue());
			if(tbUser.getTbSysRole()!=null){
				sysUserDTO.setRoleId(tbUser.getTbSysRole().getId());
			}
			conList.add(sysUserDTO);
		}

	}
	
	@Override
	public List<SysUserDTO> listUser() {
		// TODO Auto-generated method stub
		List<SysUserDTO> userDtoList = converObject(this.dao.find("from TbSysUser"));
		return userDtoList;
	}
	
	@Override
	public SysUserDTO showUser(Integer userId) {
		// TODO Auto-generated method stub
		SysUserDTO userDTO = new SysUserDTO();
		String hql="from TbSysUser user where user.id=?";
		   List userList=dao.find(hql, new Object[]{userId});
		   if(userList!=null&&userList.size()>0){
			   return getDTOFromBean((TbSysUser)userList.get(0));
		   }else{
			   return null;
		   }
	}
	
	@Override
	public void userAdd(SysUserDTO userDTO) {
		// TODO Auto-generated method stub
		this.dao.save(getBeanFromDTO(userDTO)); 
	}
	public TbSysUser queryUserInfo(Integer roleId){		     
		String hql="from TbSysUser tb where tb.tbSysRole.id=?";		     
		List  userList=dao.find(hql, new Object[]{roleId});		     
			return userList!=null&&userList.size()>0?(TbSysUser)userList.get(0):null;	
		}		/**	 * 	 * @Description  统计会员总数	 * @return	 */
	public Integer totalCount(){		  
		return dao.count();	
	}		
	public RoleServiceImpl getRoleService() {
		return roleService;
	}
	public void setRoleService(RoleServiceImpl roleService) {
		this.roleService = roleService;
	}
   
}
